$(function() {
    var Alert1 = new Alert();
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    var confirm1 = new Confirm();
    var index = {
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            $(".looktg").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName": "teacherJobManagementMessage",
                    "paramData": {
                        "chapterNum": result.paramData.chapterNum,
                        "courseId": result.paramData.courseId,
                        "classId": result.paramData.classId,
                        "chapterId": result.paramData.chapterId,
                        "some": result.paramData.some,
                        "somes": result.paramData.somes
                    }
                });
                window.location.href = "skipPage?jsonStr=" + json;
            });
            // 获取数据
            var json = JSON.stringify({"stuorteaId":result.paramData.stuorteaId,"chapterNum":result.paramData.chapterNum,"courseId":result.paramData.courseId});
            $.ajax({
                type: "post",
                url: "getMyHomeworkDetail",
                async: true,
                data: {
                    "jsonStr": json
                },
                success: function(msg) {
                    var msg = JSON.parse(msg);
                    if(msg.result == "success"){
                        var sourseMark = msg.souceMark;
                       $(".submit").attr("sum",msg.actualSumSource);
                       $("#labScore").val(msg.taskSoruce);
                        // 新增需求
                        $("#ExperimentTextarea").val(msg.lable)
                        $(".tipInfo").html(msg.tipsStr!=undefined?msg.tipsStr:'');
                       var count = msg.judgeallsource+msg.manyOptionallsource+msg.optionallsource;
                       var getSum = msg.taskSoruce;
                        if(msg.taskHtml != undefined && msg.taskHtml != null && msg.taskHtml != ''){
                            var text = unescape(msg.taskHtml)
                            text = text.replace(/\(\&emsp\;\&emsp\;\)/g,"(<b class=\"answerInt\" ></b>)");
                            text = text.replace(/\(\+\)/g,"(<b class=\"answerInt\" ></b>)");
                            $(".content").html(text);
                            $.map(JSON.parse(msg.taskAddress), function(opt) {
                                var ars = opt.ansquesContent.split(",");
                                $.each($(".questionBox[data-questionid="+ opt.questionId +"]").find(".answerInt"), function(key) {
                                    // $(this).html(opt.ansquesContent);
                                    $(this).html(ars[$(this).index()]);
                                });
                            });
                            // return msg.question.onlineSumSource;
                            var n = 0;
                            $.each( $(".shortAnswer"),function (opt) {
                                var val = $(this).val();
                                if(n != 0){
                                    val = val.substring(1);
                                }
                                n++;
                                $(this).after("<b style='padding: 0;font-weight: normal;' class='shortAnswer'>"+ val +"</b>");
                                $(this).before("<b style='font-weight: normal' class='stuAns'>学生答案：</b>");
                                $(this).remove();
                            });
                            $(".questionBox[data-questiontype=3]").append("<i class='getDisplay'>得分&ensp;:&ensp;<input maxlength='4' type='text' class='getScoreInt'><i>");
                            $(".questionBox[data-questiontype=4]").append("<i class='getDisplay'>得分&ensp;:&ensp;<input maxlength='4' type='text' class='getScoreInt'><i>");
                            maxLengthLimit.init("getScoreInt");
                            if(msg.scoreList !=undefined){
                                var arr = msg.scoreList.split(",");
                                for(var i = 0;i<$(".getScoreInt").length;i++){
                                    $(".getScoreInt").eq(i).val(arr[i]);
                                }
                            }
                            $(".getScoreInt").off("blur").blur(function () {
                                var that = $(this);
                                var numVal = $(this).val();
                                var reg = /^\d+(\.\d+)?$/;
                                if(numVal != ''){
                                    if(numVal>Number($(this).parent().parent().find(".fen").html())){
                                        Alert1.content = "请正确输入得分";
                                        Alert1.init();
                                        Alert1.show();
                                        that.val('');
                                        var counts = msg.judgeallsource+msg.manyOptionallsource+msg.optionallsource;
                                        $.each($(".getScoreInt"),function(opt){
                                            if($(this).val()!=''){
                                                counts+=Number($(this).val());
                                            }

                                        });
                                        $("#labScore").val(counts);
                                    }else{
                                        if(reg.test(numVal)){
                                            var count = msg.judgeallsource+msg.manyOptionallsource+msg.optionallsource;
                                            $.each($(".getScoreInt"),function(opt){
                                                if($(this).val()!=''){
                                                    count+=Number($(this).val());
                                                }

                                            });
                                            if(count>Number($(".submit").attr("sum"))){
                                                Alert1.content = "作业评分大于设置的作业总分数，请重新输入";
                                                Alert1.init();
                                                Alert1.show();
                                                that.val('');
                                                var counts = msg.judgeallsource+msg.manyOptionallsource+msg.optionallsource;
                                                $.each($(".getScoreInt"),function(opt){
                                                    if($(this).val()!=''){
                                                        counts+=Number($(this).val());
                                                    }

                                                });
                                                $("#labScore").val(counts);
                                            }else{
                                                $("#labScore").val(count);
                                                if(sourseMark == 0){
                                                    var json = JSON.stringify({"taskSoruce":count,"courseId":result.paramData.courseId});
                                                    $.ajax({
                                                        type: "post",
                                                        url: "getTestTaskFraction",
                                                        async: true,
                                                        data: {
                                                            "jsonStr": json
                                                        },
                                                        success: function (msg) {
                                                            var msg = JSON.parse(msg);
                                                            if (msg.result == "success") {
                                                                $(".levels").html(msg.taskFraction);
                                                                $(".levelTop").css("width",count/Number($(".submit").attr("sum"))*100+"%");
                                                                $(".downTitle").remove();
                                                                $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">作业评分: <i style="font-style: normal;color: #c80211;">'+ count +'分</i>&emsp;&emsp;&emsp;&emsp;作业评价: <i style="font-style: normal;color: #c80211;">'+ msg.taskFraction +'</i></i>');
                                                            }else{
                                                                Alert1.content = msg.failMsg;
                                                                Alert1.init();
                                                                Alert1.show();
                                                            }
                                                        }
                                                    });
                                                }else{
                                                    $(".downTitle").remove();
                                                    $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">作业评分: <i style="font-style: normal;color: #c80211;">'+ count +'分</i></i>');
                                                }

                                            }

                                        }else{
                                            Alert1.content = "请正确输入得分";
                                            Alert1.init();
                                            Alert1.show();
                                            that.val('');
                                        }
                                    }

                                }

                            });
                        }else{
                            $(".content").html('<img style="margin: 20% 0 0 37%!important;" src="../images/ad_noneResource.jpg">');
                        }
                        if(sourseMark == 0){
                            var json = JSON.stringify({"taskSoruce":Number($("#labScore").val()),"courseId":result.paramData.courseId});
                            $.ajax({
                                type: "post",
                                url: "getTestTaskFraction",
                                async: true,
                                data: {
                                    "jsonStr": json
                                },
                                success: function (msg) {
                                    var msg = JSON.parse(msg);
                                    if (msg.result == "success") {
                                        $(".levels").html(msg.taskFraction);
                                        $(".levelTop").css("width",getSum/Number($(".submit").attr("sum"))*100+"%");
                                        $(".downTitle").remove();
                                        $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">作业评分: <i style="font-style: normal;color: #c80211;">'+ getSum +'分</i>&emsp;&emsp;&emsp;&emsp;作业评价: <i style="font-style: normal;color: #c80211;">'+ msg.taskFraction +'</i></i>');
                                    }else{
                                        Alert1.content = msg.failMsg;
                                        Alert1.init();
                                        Alert1.show();
                                    }
                                }
                            });
                        }else{
                            $(".hide").hide();
                            $(".downTitle").remove();
                            $(".content").append('<i class="downTitle" style="font-style: normal;font-size:14px;position: absolute;right: 20px;top: 32px;">作业评分: <i style="font-style: normal;color: #c80211;">'+ getSum +'分</i></i>');
                        }

                        // }
                        if(msg.url != undefined && msg.url != null && msg.url != ''){
                            $(".downBtn").show();
                            $(".none").hide();
                            // $(".downBtn").attr("download",msg.homefileName);
                            $(".downBtn").attr("href",msg.url);
                        }else{
                            $(".downBtn").hide();
                            $(".none").show();

                        }
                    }else{
                        Alert1.content = msg.failMsg;
                        Alert1.init();
                        Alert1.show();
                    }


                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
            // 新增需求
            var ExperimentTextarea = $("#ExperimentTextarea");
            ExperimentTextarea.off("keyup").on("keyup", function () {
                ExperimentTextarea.keyup(function(){
                    if(ExperimentTextarea.val().length > 200){
                        ExperimentTextarea.val(ExperimentTextarea.val().substring(0,200) );
                    }
                    $("#ExperimentTextNumberSpan").text( 200 - ExperimentTextarea.val().length ) ;
                    return
                });
            })
            $(".submit").off("click").click(function () {
                if($("#labScore").val() != ''){
                    if(Number($("#labScore").val())>Number($(".submit").attr("sum"))){
                        Alert1.content = "作业评分大于设置的作业总分数，请重新输入";
                        Alert1.init();
                        Alert1.show();
                    }else{
                        var arr = [];
                        for(var i = 0;i<$(".getScoreInt").length;i++){
                            arr.push($(".getScoreInt").eq(i).val());
                        }
                        var json = JSON.stringify({
                            "chapterNum":Number(result.paramData.chapterNum),
                            "courseId":Number(result.paramData.courseId),
                            "chapterId":Number(result.paramData.chapterId),
                            "stuorteaId":result.paramData.stuorteaId,
                            "submitContent":'',
                            "taskSoruce":Number($("#labScore").val()),
                            "scoreList":arr.join(),
                            "blankscount":0,
                            "blanksallsource":0,
                            "answercount":0,
                            "answersource":0,
                            "htmlStr":escape($(".content").html()),
                            "cssStr":'@charset "utf-8";button:focus,input,input:focus,select,textarea:focus{outline:0}body,dd,dl,dt,form,h1,h2,h3,h4,h5,h6,input,li,ol,p,textarea,ul{margin:0;padding:0}select::-ms-expand{display:none;}select{border:1px solid #dcd9d9;appearance:none;-moz-appearance:none;-webkit-appearance:none;background:url(../../images/select.gif) right 10px center no-repeat #FFF}body,html{width:100%}label{font-size:16px!important;}.popup_content{font-size:16px!important;}body{font-size:16px;line-height:1.7;font-family:"Microsoft YaHei",simhei;color:#333;background-color:#DFDFDF;}input,select,textarea{color:#333;outline:none;}input{border:1px solid #dfdfdf;}input[type=checkbox]{-webkit-appearance:none;appearance:none;width:14px;vertical-align:middle;height:14px;margin:0;cursor:pointer;background:#fff;border:1px solid #a0a0a0;-webkit-border-radius:1px;-moz-border-radius:1px;border-radius:3px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:relative;}input[type=checkbox]:active,input[type=radio]:active{border-color:#a0a0a0;background:#fff;}input[type=checkbox]:hover{border-color:#a0a0a0;-webkit-box-shadow:inset 0 2px 2px rgba(0,0,0,0.1);-moz-box-shadow:inset 0 2px 2px rgba(0,0,0,0.1);box-shadow:inset 0 2px 2px rgba(0,0,0,0.1);}input[type=checkbox]:checked,input[type=radio]:checked{background:#fff;}input[type=checkbox]:checked::after{content:url(../../images/ico_ok.png);display:block;position:absolute;top:-2px;left:2px;}input[type=checkbox]:focus{outline:none;border-color:#a0a0a0;}input[type=text]:focus{outline:none;}input:-webkit-autofill{-webkit-box-shadow:0 0 0px 1000px white inset;}ul,ol{list-style:none;}a{text-decoration:none;color:#333;font-size:16px}a:visited{color:#333;}a:hover,a:active,a:focus{color:#333;}em,i{font-style:normal}b,strong{font-weight:normal;}.fl{float:left}.fr{float:right}.clear{clear:both;height:0;overflow:hidden;}img{font-size:0;display:block;overflow:hidden;}table{table-layout:fixed;}td{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}::-webkit-input-placeholder{color:#999999;}:-moz-placeholder{color:#999999;}::-moz-placeholder{color:#999999;}:-ms-input-placeholder{color:#999999;}.masking,.mb{position:fixed;width:100%;height:100%;z-index:0;background:rgba(0,0,0,0.7);top:0;left:0;display:none;}.choseOne{padding:0 15px;margin-bottom:10px;}.trueAnswer{margin-left:16px;color:#c80211;}.shortAnswer{width:50%;height:130px;resize:none;font-size:16px;padding:20px;display:none;}.answerInt{width:50px;height:20px;border:none;padding:0 5px;text-align:center;font-size:16px;min-width:50px;word-break:break-all;}.choseOne .shortAnswer{overflow:auto;border:1px solid #cdcdcd;height:500px;width:500px;margin:20px 0;text-align:left;padding:10px;display:block;}.getScoreInt{height:20px;width:50px;text-align:center;}.getDisplay{color:#c80211;margin-left:46px;display:none;}body{background:#fff;}',
                            "taskFraction":$(".levels").html(),
                            "lable": $("#ExperimentTextarea").val()
                        });
                        $.ajax({
                            type: "post",
                            url: "assessHomework",
                            async: true,
                            data: {
                                "jsonStr": json
                            },
                            success: function (msg) {
                                var msg = JSON.parse(msg);
                                if (msg.result == "success") {
                                    Alert1.content = "保存成功";
                                    Alert1.init();
                                    Alert1.show();
                                    $(".downBtn").show();
                                    $(".none").hide();
                                    $(".downBtn").attr("download",msg.homefileName);
                                    $(".downBtn").attr("href",msg.url);
                                }else{
                                    Alert1.content = msg.failMsg;
                                    Alert1.init();
                                    Alert1.show();
                                }
                            }
                        });
                    }
                }else{
                    Alert1.content = "请输入作业评分";
                    Alert1.init();
                    Alert1.show();
                }

            });
        }
    };
    index.init();

});